package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponExample;
import com.cskaoyan.market.db.domain.MarketCouponUser;
import com.cskaoyan.market.db.domain.MarketCouponUserExample;
import com.cskaoyan.market.db.mapper.MarketCouponMapper;
import com.cskaoyan.market.db.mapper.MarketCouponUserMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class AdminCouponServiceImpl implements  AdminCouPonService{
   @Autowired
   MarketCouponMapper marketCouponMapper;
   @Autowired
   MarketCouponUserMapper  marketCouponUserMapper;

   @Transactional
    @Override
    public List<MarketCoupon> getCouponList(String page, String limit, String name, String type, String status, String sort, String order) {

        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        if(StringUtils.isNotEmpty(name)){

            criteria.andNameLike("%"+name.trim()+"%");
        }
        if(StringUtils.isNotEmpty(type)){
            criteria.andTypeEqualTo(Short.parseShort(type));
        }
        if(StringUtils.isNotEmpty(status)){
            criteria.andStatusEqualTo(Short.parseShort(status));
        }
        marketCouponExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        return marketCouponMapper.selectByExampleSelective(marketCouponExample);
    }

    @Transactional
    @Override
    public void update(MarketCoupon marketCoupon) throws IOException {
        marketCoupon.setUpdateTime(LocalDateTime.now());

        marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
    }

    @Transactional
    @Override
    public void delete(MarketCoupon marketCoupon) throws JsonProcessingException {
        marketCouponMapper.deleteByPrimaryKey(marketCoupon.getId());
    }

    @Override
    public MarketCoupon getOneCouponDetail(Integer id) {
        MarketCouponExample example = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = example.createCriteria();
        if(id != null){
            criteria.andIdEqualTo(id);
        }

        return marketCouponMapper.selectOneByExample(example);
    }

    @Override
    public List<MarketCouponUser> queryUserCoupon(int page, int limit, String sort, String order, Integer couponId, Integer userId, Short status) {

        MarketCouponUserExample example = new MarketCouponUserExample();
        example.setOrderByClause(sort +" "+order);
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andCouponIdEqualTo(couponId);
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(status != null){
            criteria.andStatusEqualTo(status);
        }
        return marketCouponUserMapper.selectByExample(example);
    }

    @Override
    public int createOne(MarketCoupon marketCoupon) {

        LocalDateTime now = LocalDateTime.now();
        marketCoupon.setDeleted(false);
        marketCoupon.setAddTime(now);
        marketCoupon.setUpdateTime(now);
        return marketCouponMapper.insert(marketCoupon);
    }
}
